<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['agent_id']){
		$params['agent_id']=intval($_RQ['agent_id']);
		$return['params']['agent_id']=$_RQ['agent_id'];
	}
	if($_RQ['level_id']){
		$params['level_id']=intval($_RQ['level_id']);
		$return['params']['level_id']=$_RQ['level_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'agenttime desc',
		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['appurl']='jy_baojdai/myshop/main/mid='.$item['mid'].'&fromuser='.$item['mid'];
			$item['url']=custom_url($item['appurl'],true);
			$order_num=pdo_count('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['order_num'] +=$order_num;
			$order_price=pdo_getsum('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'price');
			$item['order_price'] +=$order_price;
			$order_num=pdo_count('jy_weishop_order',array('ispay'=>1,'status'=>3,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['isorder_num'] +=$order_num;
			$order_price=pdo_getsum('jy_weishop_order',array('ispay'=>1,'status'=>3,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'price');
			$item['isorder_price'] +=$order_price;
			//$this->model('member')->min_agent_level($item['mid']);
			$this->model('member')->update_agent_level($item['mid']);
			$downmids=$this->model('member')->getteamids($item['mid'],1);
			$downmids=$downmids?:-1;
			$item['downmids']=$downmids;
			$item['tuinum']=pdo_count('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$item['mid']));
			//$item['teamorder_price']=pdo_getsum('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$downmids),'order_price');
			$item['teamfee']=pdo_getsum('jy_baojdai_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$downmids),'order_price');
			$item['teamfee'] +=$item['teamorder_price'];
			$item['level_num']=pdo_count('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$item['level_id'],'mid'=>$downmids));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：关于匿名用户的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！</red>
		</br>1、[直推有效数]:必须成为分销商才算；（<red>所以非必要请勿手动修改上级关系，或删除代理订单</red>）。
		','key'=>'mid','edit'=>false,'psize'=>$psize,'del_one'=>true,'del_message'=>'确定删除分商商吗？会同时解除其下所有上下级关系哦！','list'=>array(
			//array('name'=>'mid','title'=>'代理商MID','type'=>'string'),
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'url','title'=>'推广二维码','type'=>'ewm','action'=>'appurl'),
			array('name'=>'level_id','title'=>'代理商等级','type'=>'select','options'=>'levels'),
			array('name'=>'stop','title'=>'代理商状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'订单金额','type'=>'string'),
			array('name'=>'isorder_num','title'=>'已完成订单数','type'=>'string'),
			array('name'=>'isorder_price','title'=>'已完成订单额','type'=>'string'),
			array('name'=>'tuinum','title'=>'直推有效数','type'=>'string'),
			array('name'=>'teamfee','title'=>'团队业绩总额','type'=>'string'),
			array('name'=>'level_num','title'=>'旗下同等级人数','type'=>'string'),
			array('name'=>'agenttime','title'=>'加入时间','type'=>'time'),
			array('name'=>'updatetime','title'=>'升级时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'恢复','show'=>array('stop'=>1)),
			array('type'=>'href','op'=>'setleve','title'=>'设置等级/有效期'),
			array('type'=>'modal','op'=>'detail','title'=>'代理商信息'),
			array('type'=>'modal','op'=>'setfee','title'=>'修改贡献值/代理额'),
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/agent/qc/','title'=>'清空小程序推广二维码缓存'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'agent_id','title'=>'选择上级'),
			array('name'=>'level_id','width'=>15,'title'=>'代理商等级','type'=>'select','options'=>'levels2'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['isbuy']=array('未消费','已消费');
	$return['agent_status']=$this->agent_status;
	$return['levels']=$return['levels2']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'会员');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>'1');
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_baojdai_member',$params);
	$agent_status=$this->agent_status;
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'会员';
			$item['stop']=$agent_status[$item['stop']];
			$item['agenttime']=date('Y-m-d H:m:s',$item['agenttime']);
		}
	}
	$header=array(
		'mid'=>'代理商MID',
		'name'=>'代理商名称',
		'mobile'=>'代理商电话',
		'agentname'=>'推荐人',
		'from_os'=>'来源',
		'level_title'=>'代理商等级',
		'agenttime'=>'成为代理商时间',
		'stop'=>'代理商状态',
		'member_num'=>'下线数',
		'agent_num'=>'下级代理商数',
		'order_num'=>'推广订单总金额',
		'order_price'=>'推广订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'代理商');
}
if($_RQ['op']=='export2'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>intval($_RQ['mid']));
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_baojdai_member',$params);
	$agent_status=$this->agent_status;
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['name']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
			$item['agentname']=$item['agent']['nickname'];
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['level_title']=$this->getcolumn('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['level_title']=$item['level_title']?:'会员';
			$item['stop']=$agent_status[$item['stop']];
			$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
		}
	}
	$header=array(
		'mid'=>'代理商MID',
		'name'=>'代理商名称',
		'mobile'=>'代理商电话',
		'agentname'=>'推荐人',
		'from_os'=>'来源',
		'level_title'=>'代理商等级',
		'createtime'=>'创建时间',
		'stop'=>'代理商状态',
		'member_num'=>'下线数',
		'agent_num'=>'下级代理商数',
		'order_num'=>'推广订单总金额',
		'order_price'=>'推广订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'代理商');
}
if($_RQ['op']=='member'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>intval($_RQ['mid']));
	$return['params']['mid']=$_RQ['mid'];
	//是否代理商
	if(isset($_RQ['is_agent'])){
		$params['is_agent']=intval($_RQ['is_agent']);
		$return['params']['is_agent']=$_RQ['is_agent'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$order_num=pdo_count('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['order_num'] +=$order_num;
			$order_price=pdo_getsum('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'price');
			$item['order_price'] +=$order_price;
			$order_num=pdo_count('jy_weishop_order',array('ispay'=>1,'status'=>3,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['isorder_num'] +=$order_num;
			$order_price=pdo_getsum('jy_weishop_order',array('ispay'=>1,'status'=>3,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'price');
			$item['isorder_price'] +=$order_price;
			//$this->model('member')->min_agent_level($item['mid']);
			$this->model('member')->update_agent_level($item['mid']);
			$downmids=$this->model('member')->getteamids($item['mid'],1);
			$downmids=$downmids?:-1;
			$item['downmids']=$downmids;
			$item['tuinum']=pdo_count('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$item['mid']));
			//$item['teamorder_price']=pdo_getsum('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$downmids),'order_price');
			$item['teamfee']=pdo_getsum('jy_baojdai_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$downmids),'order_price');
			$item['teamfee'] +=$item['teamorder_price'];
			$item['level_num']=pdo_count('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$item['level_id'],'mid'=>$downmids));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			//array('name'=>'agent','title'=>'推荐人','type'=>'member'),
			array('name'=>'level_id','title'=>'代理商等级','type'=>'select','options'=>'levels'),
			array('name'=>'stop','title'=>'代理商状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'订单金额','type'=>'string'),
			array('name'=>'isorder_num','title'=>'已完成订单数','type'=>'string'),
			array('name'=>'isorder_price','title'=>'已完成订单额','type'=>'string'),
			array('name'=>'tuinum','title'=>'直推有效数','type'=>'string'),
			array('name'=>'teamfee','title'=>'团队业绩总额','type'=>'string'),
			array('name'=>'level_num','title'=>'旗下同等级人数','type'=>'string'),
			array('name'=>'agenttime','title'=>'加入时间','type'=>'time'),
			array('name'=>'updatetime','title'=>'升级时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'恢复','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'detail','title'=>'代理商信息'),
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'会员');
	exi($return);
}
if($_RQ['op']=='stop'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->stop_agent($mid);
	exi('禁用成功！');
}
if($_RQ['op']=='start'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->start_agent($mid);
	exi('恢复成功！');
}
if($_RQ['op']=='setleve'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$update=array(
			'level_id'=>intval($post['level_id']),
			'updatetime'=>time(),
		);
		pdo_update('jy_baojdai_member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($post['agent_id']){
			$this->model('member')->update_agent_level($post['agent_id']);
		}
		exi('设置成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_baojdai_member',array('mid'=>$mid),array('agent_id','mid','level_id'));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置等级',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'等级','name'=>'level_id','options'=>'levels','help'=>'<red>设置等级，很可能不生效，因为满足条件会自动升级!</red>'),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='setfee'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$update=array(
			'redfee'=>$post['redfee'],
			'gxfee'=>$post['gxfee'],
		);
		pdo_update('jy_baojdai_member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		exi('设置成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_baojdai_member',array('mid'=>$mid),array('gxfee','mid','redfee'));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置贡献值/剩余可代理额',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'贡献值','name'=>'gxfee'),
		array('type'=>'string','title'=>'剩余可代理额','name'=>'redfee'),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='detail'){
	$mid=intval($_RQ['mid']);
	$item=pdo_get('jy_baojdai_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	$item['member']=cfc('core')->member_show($item['mid']);
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['wechat']?:$item['member']['wechat'];
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'代理商其他信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='qc'){
	$path=ROOT_AT .'oss/'.$_SESSION['uniacid'].'/images/jy_baojdai/';
	$handle = scandir($path);
	foreach($handle as $val){
    	//排除目录中的.和..
    	if($val !="." && $val !=".."){
    		//如果是文件直接删除
    		unlink($path.$val);
		}
   }
   $like='oss/' . $_SESSION['uniacid'].'/images/jy_baojdai/';
   pdo_delete('core_attachment',array('attachment like'=>'%'.trim($like).'%','uniacid'=>$_SESSION['uniacid']));
   exi('清除成功','','list');
}
if($_RQ['op']=='del'){
	$mid=intval($_RQ['mid']);
	if($mid){
		pdo_delete('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		pdo_update('jy_baojdai_member',array('agent_id'=>0),array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid));
		pdo_delete('jy_baojdai_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $mid){
			$mid=intval($mid);
			if($mid){
				pdo_delete('jy_baojdai_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
				pdo_update('jy_baojdai_member',array('agent_id'=>0),array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid));
				pdo_delete('jy_baojdai_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>